SCALE = 40;

var world, stage;
var boxes = [];


var   b2Vec2 = Box2D.Common.Math.b2Vec2
,	b2BodyDef = Box2D.Dynamics.b2BodyDef
,	b2Body = Box2D.Dynamics.b2Body
,	b2FixtureDef = Box2D.Dynamics.b2FixtureDef
,	b2Fixture = Box2D.Dynamics.b2Fixture
,	b2World = Box2D.Dynamics.b2World
,	b2MassData = Box2D.Collision.Shapes.b2MassData
,	b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
,	b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
,	b2DebugDraw = Box2D.Dynamics.b2DebugDraw
;

var library;



Box.prototype = {
	addToWorld: function(world) {
		this.world = world;

		return world.CreateBody(this.body).CreateFixture(this.fixture);
	}
};



function addWalls(world, stage) {
    var ground = new Box(0, stage.stageHeight / SCALE + 0.5, stage.stageWidth, 1);
    ground.body.type = b2Body.b2_staticBody;
    ground.addToWorld(world);

    var leftWall = new Box(-0.5, stage.stageHeight / 2 / SCALE, 1, stage.stageHeight / SCALE);
    leftWall.body.type = b2Body.b2_staticBody;
    leftWall.addToWorld(world);

    var rightWall = new Box(stage.stageWidth / SCALE + 0.5, stage.stageHeight / 2 / SCALE, 1, stage.stageHeight / SCALE);
    rightWall.body.type = b2Body.b2_staticBody;
    rightWall.addToWorld(world);

    var ceiling = new Box(stage.stageWidth / SCALE / 2, -0.5, stage.stageWidth / SCALE, 1);
    ceiling.body.type = b2Body.b2_staticBody;
    ceiling.addToWorld(world);
}

function addStar(stage) {
    
}

function main(stg) {
    getLibrary();
	//sp.bridge.send("doneloading!!!");
	var vgrav = 1.0;

    stage = stg;    // mm globalz

	world = new b2World(new b2Vec2(0.0, vgrav),  true);
    addWalls(world, stage);


    function addBox(x, y, img) {
        var box = new Player(x, y, 60 / SCALE, 60 / SCALE);
        box.body.type = b2Body.b2_dynamicBody;

        var result = {body: box.addToWorld(world), view: box.view};

        boxes.push(result);

        function removeBox(event) {
                // Don't go to stage
            event.stopImmediatePropagation();

                // remove from list
            boxes.splice(boxes.indexOf(result), 1);

                    stage.removeChild(result.view);
                    world.DestroyBody(result.body.m_body);
        }

        box.view.addEventListener(sp.TouchEvent.TOUCH_BEGIN, removeBox);
        box.view.addEventListener(sp.MouseEvent.MOUSE_DOWN, removeBox);

        stage.addChild(box.view);
    }

	// Add one box
	addBox(Math.random() * stage.stageWidth / SCALE, Math.random() * stage.stageHeight / SCALE);

	function onStageClick(event) {
		if(boxes.length > 10)
			return;

		addBox(event.stageX / SCALE, event.stageY / SCALE, "/assets/star.png");
	};

  	stage.addEventListener(sp.TouchEvent.TOUCH_BEGIN, onStageClick);
  	stage.addEventListener(sp.MouseEvent.MOUSE_DOWN, onStageClick);

	stage.addEventListener(sp.Event.ENTER_FRAME, function(event) {
		world.Step(
			1 / 30.0,	// time step
			2,	 		// velocity iterations
			2			// position iterations
		);

		for(var i=0; i<boxes.length; ++i) {
			var box = boxes[i];

			var rig = box.body.m_body.m_xf;

			var pos = rig.position;
			box.view.x = (pos.x * SCALE);
			box.view.y = (pos.y * SCALE);

			var vec = rig.R.col1;
			box.view.rotation = -Math.atan2(vec.x, vec.y) * 180.0 / Math.PI;
		}

	    world.ClearForces();
	});

	var orientation = new sp.Accelerometer();
	orientation.addEventListener(sp.AccelerometerEvent.UPDATE, function(event) {
		world.SetGravity(new b2Vec2(-event.y * SCALE / 2, -event.x * SCALE / 2));

		// Wake all boxes
		for(var i=0; i<boxes.length; ++i)
			boxes[i].body.m_body.SetAwake(true);
	});
}
